package demo.db.mybatisPlus.generator;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.converts.PostgreSqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableField;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.querys.PostgreSqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.query.SQLQuery;
import com.baomidou.mybatisplus.generator.type.ITypeConvertHandler;
import com.baomidou.mybatisplus.generator.type.TypeRegistry;
import demo.db.pg.PostgreSqlDemo;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;

import java.nio.file.Paths;
import java.sql.Types;
import java.util.Arrays;

public class MybatisPlusGeneratorDemo {


    @Test
    public void testMySQL() {
        FastAutoGenerator.create("url", "username", "password")


                .globalConfig(builder -> {
                    builder
                            .author("Baomidou")// 设置作者
                            .outputDir(Paths.get(System.getProperty("user.dir")) + "/src/main/java")
                            .commentDate("yyyy-MM-dd")
                            .enableSwagger(); // 开启 swagger 模式
                })
                .dataSourceConfig(builder ->
                        builder.databaseQueryClass(SQLQuery.class)
                                .typeConvert(new MySqlTypeConvert())
                                .dbQuery(new MySqlQuery())
                )
                .packageConfig(builder -> builder
                        .parent("com.baomidou.mybatisplus")
                        .entity("entity")
                        .mapper("mapper")
                        .service("service")
                        .serviceImpl("service.impl")
                        .xml("mapper.xml")
                )
                .strategyConfig(builder -> builder
                        .entityBuilder()
                        .enableLombok()
                )
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }


    @Test
    public void testPostgreSQL() {
        FastAutoGenerator.create(PostgreSqlDemo.jdbcUrl, PostgreSqlDemo.user, PostgreSqlDemo.password)
                .globalConfig(builder -> {
                    builder.author("junying.han")// 设置作者
                            .outputDir(Paths.get(System.getProperty("user.dir")) + "/src/main/java")
                            .commentDate("yyyy-MM-dd")
                            .enableSwagger(); // 开启 swagger 模式
                })
                .dataSourceConfig(builder ->
                        builder.databaseQueryClass(SQLQuery.class)
                                .typeConvert(new PostgreSqlTypeConvert())
                                .dbQuery(new PostgreSqlQuery())
                                .typeConvertHandler(new ITypeConvertHandler() {
                                    @Override
                                    public @NotNull IColumnType convert(GlobalConfig globalConfig, TypeRegistry typeRegistry, TableField.MetaInfo metaInfo) {
                                        int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                                        if (typeCode == Types.SMALLINT) {
                                            // 自定义类型转换
                                            return DbColumnType.INTEGER;
                                        }
                                        return typeRegistry.getColumnType(metaInfo);
                                    }
                                })
                )
                .packageConfig(builder -> builder
                        .parent("com.ximalaya.xdcs.dify")
                        .entity("pojo")
                        .mapper("mapper")
                        .service("service")
                        .serviceImpl("service.impl")
                        .xml("mapper.xml")
                )
                .strategyConfig(builder -> builder
                        .addInclude(Arrays.asList("accounts", "apps"))// 设置需要生成的表名
                        .entityBuilder()
                        .enableLombok()
                )
                .templateEngine(new FreemarkerTemplateEngine())// 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
